package com.d3works.csv.script;

import com.d3works.apps.production.db.BillOfMaterial;
import com.d3works.apps.production.db.repo.BillOfMaterialRepository;
import com.d3works.apps.production.service.BillOfMaterialService;
import com.d3works.apps.production.service.costsheet.CostSheetService;
import com.d3works.exception.D3worksException;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import java.util.Map;

public class ImportBillOfMaterial {

  @Inject BillOfMaterialService billOfMaterialService;

  @Inject CostSheetService costSheetService;

  @Inject BillOfMaterialRepository bomRepo;

  @Transactional(rollbackOn = {Exception.class})
  public Object computeCostPrice(Object bean, Map values) throws D3worksException {
    if (bean == null) {
      return bean;
    }
    assert bean instanceof BillOfMaterial;
    BillOfMaterial bom = (BillOfMaterial) bean;
    bom = bomRepo.save(bom);
    if (bom.getDefineSubBillOfMaterial()) {
      costSheetService.computeCostPrice(bom, CostSheetService.ORIGIN_BILL_OF_MATERIAL, null);
      billOfMaterialService.updateProductCostPrice(bom);
    }
    return bom;
  }
}
